package com.kuaiprint.main;



import java.util.List;

import com.kuaiprint.fragment.PrintStoreFragment;
import com.kuaiprint.fragment.PrintStructFragment;
import com.kuaiprint.fragment.UserCenterFragment;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity
{
	private SlidingMenu mMenu;
	private RelativeLayout userCenter;
	private RelativeLayout printMethod;
	private RelativeLayout printStore;
	private TextView topTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		mMenu = (SlidingMenu) findViewById(R.id.id_menu);
		topTitle = (TextView) findViewById(R.id.top_title);
		topTitle.setVisibility(View.GONE);
		topTitle.setText("打印店铺");
		topTitle.setVisibility(View.VISIBLE);
		openFragment(PrintStoreFragment.class,null,PrintStoreFragment.class.getName(),false);
		userCenter = (RelativeLayout) findViewById(R.id.user_center);
		userCenter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenu.toggle();
				topTitle.setText("个人中心");
				topTitle.setVisibility(View.VISIBLE);
				openFragment(UserCenterFragment.class,null,UserCenterFragment.class.getName(),false);	
			}
		});
		
		printMethod=(RelativeLayout) findViewById(R.id.print_method);

		printMethod.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenu.toggle();
				topTitle.setText("新印单");
				topTitle.setVisibility(View.VISIBLE);
				
				openFragment(PrintStructFragment.class,null,PrintStructFragment.class.getName(),false);
			
			}
		});
		printStore = (RelativeLayout) findViewById(R.id.print_store);
		printStore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mMenu.toggle();
				topTitle.setText("打印店铺");
				topTitle.setVisibility(View.VISIBLE);
				openFragment(PrintStoreFragment.class,null,PrintStoreFragment.class.getName(),false);
			}
		});
		
	}

	public void toggleMenu(View view)
	{
		mMenu.toggle();
	}
	
	public void openFragment(Class<? extends Fragment> clazz, Bundle args, String tag, boolean addToBackStack) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		if (fragment == null || fragment.isRemoving()) {
			fragment = Fragment.instantiate(this, clazz.getName(), args);
		}
		if (addToBackStack) {
			ft.addToBackStack("F#" + System.currentTimeMillis());
		}

		List<Fragment> fragmentList = fragmentManager.getFragments();
		if (fragmentList != null && fragmentList.size() > 0) {
			ft.setCustomAnimations(R.anim.side_in_right, R.anim.side_out_left, R.anim.side_in_left, R.anim.side_out_right);
		}
		ft.replace(R.id.content, fragment, tag);
		ft.commit();
	}

	
}
